package reflect_t

import (
	"fmt"
	"reflect"
)

type User struct {
	Id   int
	Name string "学生姓名"
	Age  int    `a:"111"b:"2222"`
}

func (t User) String() {
	println("user: ", t.Id, t.Name, t.Age)
}

func Info(o interface{}) {
	v := reflect.ValueOf(o)
	t := v.Type()
	println(t.Name())
	println("Fields:")
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		value := v.Field(i).Interface()
		switch value := value.(type) {
		case int:
			fmt.Printf("%6s, %v = %d \n", field.Name, field.Type, value)
		case string:
			fmt.Printf("%6s, %v = %s \n", field.Name, field.Type, value)
		default:
			fmt.Printf("default, %6s, %v = %d \n ", field.Name, field.Type, value)
		}
	}
}

func ref_val() {
	u := User{1, "Tom", 30}
	Info(u)
}

func TypeValue() {
	//
	u := User{1, "Tom", 30}
	//value to type

	value := reflect.ValueOf(u)
	//reflect.ValueOf(u).Type()

	value.Interface()

	value.Int()

	//type0 := value.Type()

}

func valDemo() {
	u := User{1, "Tom", 30}
	va := reflect.ValueOf(u)
	vb := reflect.ValueOf(&u)

	fmt.Println(va.CanSet(), va.FieldByName("Name").CanSet())

	fmt.Println(vb.CanSet(), vb.Elem().FieldByName("Name").CanSet())

	fmt.Printf("%v  --\n", vb)

	name := "shine"
	vc := reflect.ValueOf(name)
	vb.Elem().FieldByName("Name").Set(vc)

	fmt.Printf("%v --\n", vb)

}
